package st.business.tepth.business.goodscheck;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.MaterialCheckBean;
import st.business.tepth.business.event.GoodsEvent;
import st.business.tepth.business.goodscheck.entity.Level0;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;
import st.shadow.library.ui.recycler.BaseDecoration;

import static st.business.tepth.business.goodsorder.GoodsOrderAdapter.TYPE_LEVEL_0;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-26 15:58
 * 物料审核
 */

public class GoodsCheckDelegate extends MvpFragment<GoodsCheckPresenter> implements GoodsCheckContract.View {
    public static GoodsCheckDelegate newInstance() {
        return new GoodsCheckDelegate();
    }

    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private GoodsCheckAdapter mCheckAdapter;
    private DialogLoader mDialogLoader;

    @Override
    public GoodsCheckPresenter getPresenter() {
        return new GoodsCheckPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_check;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("审核列表");
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 2));
        mRecyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter,
                                               View view, int i) {
                Level0 level0 = (Level0) baseQuickAdapter.getItem(i);
                if (level0 != null) {
                    MaterialCheckBean goodsOrderBean = level0.getGoodsOrderBean();
                    mPresenter.check(UserManager.getInstance().getToken(),
                            new GoodsCheckConvert().convertToIDBeanString(goodsOrderBean),
                            goodsOrderBean.getTaskId(), view.getId() == R.id.tv_no_pass ?
                                    2 : 1);
                    mDialogLoader.showProgressDialog(true, "正在提交",
                            "请稍候...");
                }
            }
        });
        mCheckAdapter = new GoodsCheckAdapter(null);
        mCheckAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                MultiItemEntity entity = mCheckAdapter.getItem(position);
                if (entity != null) {
                    return entity.getItemType() == TYPE_LEVEL_0 ? 2 : 1;
                } else {
                    return 2;
                }
            }
        });
        mRecyclerView.setAdapter(mCheckAdapter);
        mCheckAdapter.setEmptyView(R.layout.view_loading,
                (ViewGroup) mRecyclerView.getParent());
        mDialogLoader = new DialogLoader(_mActivity);
        mPresenter.getData(UserManager.getInstance().getToken());
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public void showData(List<MaterialCheckBean> list) {
        mCheckAdapter.setEmptyView(R.layout.view_search_empty, (ViewGroup) mRecyclerView.getParent());
        mCheckAdapter.setNewData(new GoodsCheckConvert().convertToEntity(list));
    }

    @Override
    public void checkSucceed(final String msg) {
        _mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mDialogLoader.stopProgressWithSuccess(msg, null, new IDialogClickListener() {
                    @Override
                    public void onClick() {
                        mDialogLoader.clearDialog();
                    }
                });
            }
        });
        EventBus.getDefault().post(new GoodsEvent());
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed(msg, null, new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
            }
        });
    }
}